/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.sfo.viewer;


import idea.sfo.viewer.utilites.Ruler;
import idea.sfo.viewer.utilites.Corner;
import idea.sfo.viewer.utilites.ToolBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.ScrollPaneConstants;
import org.openide.nodes.Children;
import org.openide.nodes.Node;


/**
 *
 * @author renke002
 */
public class CanvasPanel extends JPanel
{

    /**
     * The canvas.
     */
    private Canvas canvas;
    /**
     * The tool bar.
     */
    private final ToolBar toolbar;
    /**
     *
     */
    private final boolean verbose;


    /**
     * Creates new form CanvasPanel
     */
    public CanvasPanel( SfoViewerTopComponent parent , boolean verbose )
    {
        initComponents();
        //
        this.verbose = verbose;
        //
        this.toolbar = new ToolBar( parent );

        // The canvas is where we paint the elements from the SFO's XML file.
        this.canvas = new Canvas( this.toolbar , parent , verbose );

        //
        this.toolbar.setCanvas( this.canvas );
        JScrollPane canvasPane = new JScrollPane( this.canvas , ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS , ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS );
        //
        Ruler hRule = new Ruler( Ruler.HORIZONTAL , false );
        Ruler vRule = new Ruler( Ruler.VERTICAL , false );

        hRule.setPreferredHeight( 24 );
        vRule.setPreferredWidth( 24 );

        canvasPane.setColumnHeaderView( hRule );
        canvasPane.setRowHeaderView( vRule );

        JPanel buttonCorner = new JPanel(); //use FlowLayout
        JToggleButton isMetric = new JToggleButton( "cm" , true );
        isMetric.setFont( new Font( "SansSerif" , Font.PLAIN , 11 ) );
        isMetric.setMargin( new Insets( 2 , 2 , 2 , 2 ) );
        //      isMetric.addItemListener( this );
        buttonCorner.add( isMetric );

        canvasPane.setCorner( JScrollPane.UPPER_LEFT_CORNER , buttonCorner );
        canvasPane.setCorner( JScrollPane.UPPER_RIGHT_CORNER , new Corner() );
        canvasPane.setCorner( JScrollPane.LOWER_LEFT_CORNER , new Corner() );
        canvasPane.setCorner( JScrollPane.LOWER_RIGHT_CORNER , new Corner() );

        this.add( canvasPane , BorderLayout.CENTER );
        this.add( toolbar , BorderLayout.NORTH );
    }


    /**
     *
     */
    public void clearGraphics()
    {
        this.canvas.clear();
    }


    /**
     *
     * @param n
     * @return
     */
    public Node getNodeParent( Node n )
    {
        return this.canvas.getNodeParent( n );
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        setLayout( new java.awt.BorderLayout() );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    /**
     * Turn on or off each element's border.
     *
     * @param borderVisible
     */
    public void setBorderVisible( boolean borderVisible )
    {
        this.canvas.setBordersVisible( borderVisible );
    }


    /**
     * Set the canvas's background color to the one passed in as parameter.
     *
     * @param color
     */
    public void setCanvasColor( Color color )
    {
        this.canvas.setBackGroundColor( color );
    }


    public void setGrahpicObjects( Children.Keys ck )
    {
        this.canvas.setGrahpicObjects( ck );
    }


    /**
     * Set the node(s) passed in as parameter as selected/highlighted.
     *
     * @param selectedNodes
     */
    public void setSelectedNodes( Node[] selectedNodes )
    {
        this.canvas.setSelectedNodes( selectedNodes );
    }

}
